#!/bin/bash
# 银河麒麟桌面操作系统-系统信息获取脚本
# version: 0.2
# Author: rechie
# Email: gaoruiqi@kylinos.cn
# Date: 2022-01-07

# 限制必须有root权限才能执行脚本
[ $(id -u) -ne 0 ] && echo "该工具必须使用root账号,否则某些项无法获取" && exit 1

is_kylinos(){
# 判断是否是银河麒麟桌面操作系统V10SP1
version=$(cat /etc/os-release | grep -w "银河麒麟桌面操作系统V10"| awk -F"=" '{print $2}')
if [ "$version" ];then
    echo "操作系统名称: $version" && cat /etc/kylin-build
else
    echo "不支持的操作系统"
    exit 1
fi
echo ""
}

get_kylin_serial(){
# 查看服务序列号
kylin_serial=$(kylin_serial)
if [ "$kylin_serial" ];then
    echo "服务序列号: $kylin_serial"
else
    echo "服务序列号: Not Specified"
fi
echo ""
}

get_kyactivation_key(){
# 查看激活码
kyactivation_key=$(cat /etc/.kyactivation 2>/dev/null)
if [ "$kyactivation_key" ];then
    echo "激活码: $kyactivation_key"
else
    echo "激活码: Not Specified"
fi
echo ""
}

get_kyregistration_key(){
# 注册码
kyregistration_key=$(kylin_gen_register)
if [ "$kyregistration_key" ];then
    echo "注册码: $kyregistration_key"
else
    echo "注册码: Not Specified"
fi
echo ""
}

get_system_serial_number(){
# 查看主机序列号
system_serial_number=$(cat /sys/class/dmi/id/product_serial)
if [ "$system_serial_number" ];then
    echo "主机序列号: $system_serial_number"
else
    echo "主机序列号: Not Specified"
fi
echo ""
}

get_blk_serials(){
# 查看硬盘序列号
blk_serials=$(lsblk -d -o NAME,SERIAL,TYPE -P|grep disk)
if [ "$blk_serials" ];then
    echo "硬盘序列号:" && echo $blk_serials
fi
# 插入空行
echo ""
}

get_mac(){
#查看mac地址
echo "网卡MAC地址:"
phy_adapters=$(ls /sys/class/net/ | grep -v "$(ls /sys/devices/virtual/net/)")
for phy_adapter in $phy_adapters
do
    mac=$(cat /sys/class/net/$phy_adapter/address)
    echo "NAME=\"$phy_adapter\" MAC=\"$mac\""
done
}

get_system_info()
{
is_kylinos
get_kylin_serial
get_kyactivation_key
get_kyregistration_key
get_system_serial_number
get_blk_serials
get_mac
}

[ -e "/tmp/kylin_system_info.txt" ] && rm -f "/tmp/kylin_system_info.txt"
get_system_info | tee -a "/tmp/kylin_system_info.txt"
